package org.itzheng.yuezhi.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.itzheng.yuezhi.R;
import org.itzheng.yuezhi.adapter.BaseRecyclerViewAdapter;
import org.itzheng.yuezhi.base.BaseActivity;
import org.itzheng.yuezhi.constant.Extras;
import org.itzheng.yuezhi.constant.HttpUrl;
import org.itzheng.yuezhi.entity.SearchNewsEntity;
import org.itzheng.yuezhi.utils.JsonUtils;
import org.itzheng.yuezhi.utils.MyLog;
import org.itzheng.yuezhi.utils.MyOkHttp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 新闻列表
 */
public class NewsListActivity extends BaseActivity {
    private static final String TAG = "NewsListActivity";
    Intent intent;
    String channelName;
    String channelId;
    @BindView(R.id.tv_template_title_name)
    TextView tv_template_title_name;
    @BindView(R.id.rcv_news_list_content)
    RecyclerView rcv_news_list_content;
    @BindView(R.id.srl_news_list_content)
    SwipeRefreshLayout srl_news_list_content;
    /**
     * 新闻列表集合
     */
    List<SearchNewsEntity.ShowapiResBody.Pagebean.Contentlist> contentlist = new ArrayList<>();
    NewsListAdapter newsListAdapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);
        ButterKnife.bind(this);
        intent = getIntent();
        channelName = intent.getStringExtra(Extras.CHANNEL_NAME);
        channelId = intent.getStringExtra(Extras.CHANNEL_ID);
        tv_template_title_name.setText(channelName + "");
        initData();
    }

    private void initData() {
        page = 1;
        //设置刷新事件
        srl_news_list_content.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        getNetData();
    }

    private void getNetData() {
        HashMap<String, Object> hashMap = new HashMap();
        hashMap.put("channelId", channelId);
        hashMap.put("page", page);
        MyOkHttp.getInstance().getUrl(HttpUrl.SEARCH_NEWS, new MyOkHttp.Header("apikey", HttpUrl.BAIDU_API_KEY), hashMap, new MyOkHttp.RequestCallBack() {
            @Override
            public void onFailure(Exception httpException, String describe) {
                parseObject(describe);
            }

            @Override
            public void onSuccess(String result) {
//                ALog.i("WX_ARTICLE_SEARCH=" + result);
                MyLog.d(TAG, result);
//                MyLog.d(result);
                parseObject(result);
            }
        });
    }

    private void parseObject(String result) {
        SearchNewsEntity searchNewsEntity = JsonUtils.fromJson(result, SearchNewsEntity.class);
        if (page == 1) {
            contentlist.clear();
        }
        if (searchNewsEntity == null || searchNewsEntity.getShowapi_res_body() == null
                || searchNewsEntity.getShowapi_res_body().getPagebean() == null) {
            //显示没有数据的布局
            MyLog.e(TAG, "searchNewsEntity is null ");
        } else {
            contentlist.addAll(searchNewsEntity.getShowapi_res_body().getPagebean().getContentlist());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                initView();
            }
        });


    }

    private void initView() {
        if (newsListAdapter == null) {
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
            rcv_news_list_content.setLayoutManager(layoutManager);
            newsListAdapter = new NewsListAdapter(this, contentlist);
            rcv_news_list_content.setAdapter(newsListAdapter);
        } else {
            newsListAdapter.notifyDataSetChanged();
        }
        //结束刷新
        srl_news_list_content.setRefreshing(false);
    }

    public class NewsListAdapter extends BaseRecyclerViewAdapter {
        Activity mActivity;
        List<SearchNewsEntity.ShowapiResBody.Pagebean.Contentlist> mContentlist;

        public NewsListAdapter(Activity activity, List<SearchNewsEntity.ShowapiResBody.Pagebean.Contentlist> contentlist) {
            super();
            mActivity = activity;
            mContentlist = contentlist;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view;
            switch (viewType) {
                default:
                    view = View.inflate(mActivity, R.layout.item_news_list, null);
                    NewsListViewHolder newsListViewHolder
                            = new NewsListViewHolder(view);
                    newsListViewHolder.tv_news_list_title = (TextView) view.findViewById(R.id.tv_news_list_title);
                    newsListViewHolder.tv_news_list_desc = (TextView) view.findViewById(R.id.tv_news_list_desc);
                    newsListViewHolder.tv_news_list_put_date = (TextView) view.findViewById(R.id.tv_news_list_put_date);
                    newsListViewHolder.tv_news_list_source = (TextView) view.findViewById(R.id.tv_news_list_source);
                    return newsListViewHolder;

            }
        }

        @Override
        public int getItemCount() {
            if (mContentlist == null || mContentlist.isEmpty()) {
                return 0;
            }
            return mContentlist.size();
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position, boolean isBaseRecyclerViewAdapter) {
            if (holder instanceof NewsListViewHolder) {
                final SearchNewsEntity.ShowapiResBody.Pagebean.Contentlist contentlist = mContentlist.get(position);
                NewsListViewHolder newsListViewHolder = (NewsListViewHolder) holder;
                newsListViewHolder.tv_news_list_title.setText(contentlist.getTitle());
                newsListViewHolder.tv_news_list_desc.setText(contentlist.getDesc());
                newsListViewHolder.tv_news_list_put_date.setText(contentlist.getPubDate());
                newsListViewHolder.tv_news_list_source.setText("来源:" + contentlist.getSource());
                newsListViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mActivity, WebViewActivity.class);
                        intent.putExtra("url", contentlist.getLink() + "");
                        mActivity.startActivity(intent);
                    }
                });
            }

        }

        class NewsListViewHolder extends RecyclerView.ViewHolder {
            TextView tv_news_list_title;
            TextView tv_news_list_desc;
            TextView tv_news_list_put_date;
            TextView tv_news_list_source;

            public NewsListViewHolder(View itemView) {
                super(itemView);
            }
        }
    }
}
